<template>
	<view>
		<view class="image" v-if="business_license != ''">
			<image :src="business_license" mode="widthFix"></image>
		</view>
		<view class="button">
			<button @click="openSelectImage">上传营业执照</button>	
		</view>		
	</view>
</template>

<script>
	import {
	  apiDistributionIndex,
	  apiDistributionApply,
	  apiDistributionApplyDetail,
	  apiDistributionCode,
	  apiModifyBusinessLicense
	} from "@/api/user";
	import { chooseImage, uploadFile, trottle } from '@/utils/tools.js'
	export default{
		data(){
			return {
				business_license:""
			}
		},
		methods:{
			async getDistributionIndex() {
			  apiDistributionIndex().then((res) => {			  
				this.business_license = res.business_license
			    this.loading = false;
			  });
			},
			
			openSelectImage() {
				uni.chooseImage({
					sizeType: ['compressed'],
					sourceType: ['album', 'camera'],
					success: (res) => {						
						if (res.tempFilePaths?.length > 0) {
							uploadFile(res.tempFilePaths[0])
							    .then((res) => {
							        uni.hideLoading()
									let url = res.url
									apiModifyBusinessLicense({"business_license":url}).then(()=>{
										this.$toast({
										    title: '上传成功'
										})
										this.getDistributionIndex()
									}).catch((err) => {
										this.$toast({
										    title: '上传失败1'
										})
									})								
							    })
							    .catch((err) => {									
							        uni.hideLoading()
							        this.$toast({
							            title: '上传失败2'
							        })
							    })
						}
					},
					fail: (err) => {
						console.log("err: ------", err);
					}
				})
			},
		},
		onLoad(){
			this.getDistributionIndex()
		}
	}
</script>

<style>
	.image{
		float: left;
		width: 700rpx;
		margin-left: 25rpx;
		margin-right: 25rpx;
	
	}
	.image > image{
		float: left;
		width: 100%; /* 图片宽度自适应容器宽度 */
		height: auto; /* 图片高度自适应宽度 */
		
	}
	.button{
		float: left;
		margin-top: 30rpx;
		width: 700rpx;
		margin-left: 25rpx;
		margin-right: 25rpx;
	}
	
</style>